<?php

namespace App\Http\Controllers\Guard\Car;

use App\Http\Controllers\Common\GuardController;
use App\Models\User\User;
use Illuminate\Http\Request;

class UserController extends GuardController {

    public function __construct() {
        $this->instance = new User();
    }

    public function lst(Request $request) {
        $conditions = $request->input();
        $records = $this->instance->getLst($conditions)
            ->with('getRentUnit:id,unit_name')
            ->paginate($conditions['pageSize'],$this->getCol,'page',$conditions['page']);
        return responseSuccess($records->items(),$records->total());
    }

    public function add(Request $request) {
        $conditions = $request->input();
        $this->validate($request, [
            'name'=>'required',
            'password'=>'required',
            'phone'=>['required','regex:/^1[345789][0-9]{9}$/'],
            'rent_unit_id'=>'required',
        ],[],[
            'name'=>'姓名',
            'rent_unit_id'=>'租借单位',
        ]);
        $id = $request->input('id',0);
        $conditions['password_md5'] = md5($conditions['password']);
        $user = $this->instance->updateOrCreate(compact('id'),$conditions);
        return responseSuccess($user->toArray());
    }

    public function detail(Request $request){
        $id = $request->input('id',0);
        $user = User::find($id);
        return responseSuccess($user->toArray());
    }
}
